// 获取当前帐号信息
const accountInfo = wx.getAccountInfoSync();
// env类型
const env = accountInfo.miniProgram.envVersion;
const baseApi = {
  // 开发版
  // develop: "http://localhost:3000",
  // develop: "http://192.168.1.6:3000",
  develop: "http://gbm666.top:3000",
  // develop: "https://autumnfish.cn",
  // 体验版
  trial: "https://autumnfish.cn",
  // 正式版
  release: "https://autumnfish.cn"
};
// request请求baseURL
const apiBaseUrl = baseApi[env] || ''; //
function request(url, data, method) {
  return new Promise(function (resolve, reject) {
    wx.request({
      url: apiBaseUrl + url,
      data: data,
      method: method,
      header: {
        'Content-Type': 'application/json',
        // 'X-Dts-Token': wx.getStorageSync('token')
      },
      timeout: 15000,
      success: function (res) {
        if (res.statusCode == 200) {
          if (res.data.code == 501) {
            // 清除登录相关内容
            try {
              wx.removeStorageSync('userinfo');
              wx.removeStorageSync('openid');
            } catch (e) {
              // Do something when catch error
            }
            // 切换到登录页面
            wx.navigateTo({
              url: '/pages/index/index'
            });
          } else {
            resolve(res.data);
          }
        } else {
          reject(res.errMsg);
        }
      },
      fail: function (err) {
        reject(err)
      }
    })
  });
}
module.exports = {
  request,
}